package org.skywork.top.core.algorithm.str;

import java.util.*;

/**
 * 电话号码的字母组合
 */
public class no_17 {

    /**
     * 给定一个仅包含数字 2-9 的字符串，返回所有它能表示的字母组合。
     * 答案可以按 任意顺序 返回。
     * 给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母。
     */
    public List<String> letterCombinations(String digits) {

        /**
         * 示例 1：
         * 输入：digits = "23"
         * 输出：["ad","ae","af","bd","be","bf","cd","ce","cf"]
         *
         * 示例 2：
         * 输入：digits = ""
         * 输出：[]
         *
         * 示例 3：
         * 输入：digits = "2"
         * 输出：["a","b","c"]
         */
        Map<Integer, List<String>> map = new HashMap<>();
        map.put(1, new ArrayList<>());
        map.put(2, Arrays.asList("a", "b", "c"));
        map.put(3, Arrays.asList("d", "e", "f"));

        if ("".equals(digits)) {
            return new ArrayList<>();
        }
        if (digits.length() == 1) {
            return map.get(Integer.parseInt(digits));
        }

        String[] str = digits.split("");
        Map<Integer, List<String>> map2 = new HashMap<>();
        for (int i = 0; i < str.length; i++) {
            int num = Integer.parseInt(str[i]);
            if (num == 0 || num == 1) {
                continue;
            }
            map2.put(num, map.get(num));
        }

        return null;
    }


    private String letterMap[] = {
            " ",    //0
            "",     //1
            "abc",  //2
            "def",  //3
            "ghi",  //4
            "jkl",  //5
            "mno",  //6
            "pqrs", //7
            "tuv",  //8
            "wxyz"  //9
    };

    private ArrayList<String> res;

    public List<String> letterCombinations2(String digits) {
        res = new ArrayList<String>();
        if (digits.equals(""))
            return res;

        findCombination(digits, 0, "");
        return res;
    }

    private void findCombination(String digits, int index, String s) {
        if (index == digits.length()) {
            res.add(s);
            return;
        }
        char c = digits.charAt(index);
        String letters = letterMap[c - '0'];
        for (int i = 0; i < letters.length(); i++) {
            findCombination(digits, index + 1, s + letters.charAt(i));
        }
    }
}
